package com.booksystem.campuslibrarymanagementsystem.controller.approve;

import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.Booksearch;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.BookselectR;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.AccountTable;
import com.booksystem.campuslibrarymanagementsystem.entity.BooksTable;
import com.booksystem.campuslibrarymanagementsystem.entity.CategoriesTable;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.service.BooksTableService;
import com.booksystem.campuslibrarymanagementsystem.service.CategoriesTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/approval")
@Api(tags = "库存查询")
@RestController
public class InventoryViewingController {

    BooksTable booksTable= new BooksTable();
    @Autowired
    BooksTableService booksTableService;
    @Autowired
    AccountTableService accountTableService;
    @Autowired
    CategoriesTableService categoriesTableService;

        /**
         *
//         * @param booksearch 图书搜索
         * 1,检查至少有一个搜索要求，没，提示请输入搜索条件
         * 2，单个条件搜索
         * 3，两个条件搜索
         *4，三个条件搜索
         *     获得图书类别到类别表，得到图书id,再通过图书id在图书表获得图书信息
         * @return
         */

        @PostMapping("/BookQuery")
        @ApiOperation(value = "库存查询",notes = "按照图书名称，图书类别，作者，进行库存查询功能")
        public Result<ArrayList<BookselectR>> booksearch (@RequestBody Booksearch bookselect) {
            Map<String, Object> parsed = JwtUtil.tokenPre(bookselect.getToken());
            if (parsed != null) {
                Object identity = parsed.get("identity");
                Object account = parsed.get("account");
                if (identity.equals("approval")) {
                    AccountTable accountTable = accountTableService.queryByAccount((String) account);
                    if (accountTable != null) {
                        if (bookselect.getCategory().isEmpty()) {
                            List<BooksTable> list = booksTableService.selectbookList(bookselect.getTitle(), bookselect.getAuthor(), bookselect.getCategory());
                            if (!list.isEmpty()) {
                                ArrayList<BookselectR> list1 = new ArrayList<>();
                                for (int i = 0; i < list.size(); i++) {
                                    booksTable = list.get(i);
                                    BookselectR bookselectR = new BookselectR();
                                    bookselectR.setBookid(booksTable.getBookid());
                                    bookselectR.setTitle(booksTable.getTitle());
                                    bookselectR.setAuthor(booksTable.getAuthor());
                                    bookselectR.setCategory(booksTable.getCategory());
                                    bookselectR.setTotalcopies(booksTable.getTotalcopies());
                                    bookselectR.setAvailablecopies(booksTable.getAvailablecopies());
                                    list1.add(bookselectR);
                                }
                                return Result.success(list1, list1.size());
                            } else {
                                return Result.fail("没有找到符合的图书信息");
                            }
                        } else {
                            CategoriesTable categoriesTable = categoriesTableService.queryByCategoryName(bookselect.getCategory());
                            if (categoriesTable != null) {
                                List<BooksTable> list = booksTableService.selectbookList(bookselect.getTitle(), bookselect.getAuthor(), bookselect.getCategory());
                                if (!list.isEmpty()) {
                                    ArrayList<BookselectR> list1 = new ArrayList<>();
                                    for (int i = 0; i < list.size(); i++) {
                                        booksTable = list.get(i);
                                        BookselectR bookselectR = new BookselectR();
                                        bookselectR.setBookid(booksTable.getBookid());
                                        bookselectR.setTitle(booksTable.getTitle());
                                        bookselectR.setAuthor(booksTable.getAuthor());
                                        bookselectR.setCategory(booksTable.getCategory());
                                        bookselectR.setTotalcopies(booksTable.getTotalcopies());
                                        list1.add(bookselectR);
                                    }
                                    return Result.success(list1, list1.size());
                                } else {
                                    return Result.fail("没有找到符合的图书信息");
                                }
                            } else {
                                return Result.fail("没有找到基于图书类别");
                            }
                        }

                    } else {
                        return Result.fail("该学号不存在");
                    }
                } else {
                    return Result.fail("身份权限不足");
                }
            } else {
                return Result.fail("请输入正确token值");
            }
        }
}




